package com.ahsjdy.st.stbocustomer.task;

import com.ahsjdy.stcommon.enums.OrderEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author xuetao wang
 * @Date 2020/2/9
 */
@Component
public class PayCancelTask {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CustomerServiceImpl customerService;

    /**
     * 检查过期的订单
     */
    @Scheduled(cron = "0/58 * * * * *")
    public void cancelPayOrder() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, -15);

        Date beginTime = cal.getTime();

        List<Order> orders = orderMapper.list4CancelPay(OrderEnum.待付款.getK(), beginTime);
        for (Order order : orders) {
            try {
                customerService.cancelPayOrder(order);
                System.out.println(">>>>订单" + order.getId() + "过期");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
